package com.lau.videoeffect.camera

import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.lau.videoeffect.VideoEffect
import com.lau.videoeffect.VideoEffectJni
import com.lau.videoeffect.utils.GLTexture

class CameraActivity : AppCompatActivity() {

    companion object {
        private const val PERMISSION_REQUEST_CAMERA = 0
        private const val PERMISSION_REQUEST_STORAGE = 1
    }

    var mCameraView: CameraView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        checkPermissions()
    }

    override fun onResume() {
        super.onResume()
        mCameraView?.onResume()
    }

    override fun onPause() {
        super.onPause()
        mCameraView?.onPause()
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        when (requestCode) {
            PERMISSION_REQUEST_CAMERA -> {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    checkPermissions()
                } else {
                    Toast.makeText(this, "Camera权限被拒绝", Toast.LENGTH_SHORT).show()
                }
            }
            PERMISSION_REQUEST_STORAGE -> {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    checkPermissions();
                } else {
                    Toast.makeText(this, "Storage权限被拒绝", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    private fun checkPermissions() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(arrayOf(Manifest.permission.CAMERA), PERMISSION_REQUEST_CAMERA)
            } else if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(
                    arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
                    PERMISSION_REQUEST_STORAGE
                )
            } else {
                setCameraView()
            }
        } else {
            setCameraView()
        }
    }

    private fun setCameraView() {
        Log.d("MAIN", "setCameraView()")
        mCameraView = CameraView(this)
        mCameraView?.setDrawFrameCallback(720, 1280, object : CameraView.DrawFrameCallback {
            override fun onDrawFrame(textureIn: GLTexture, textureOut: GLTexture) {
//                mCameraView?.copyTexture(textureIn, textureOut)
                val veTextureIn = VideoEffect.VE_Texture()
                veTextureIn.textureID = textureIn.textureId
                veTextureIn.format = textureIn.format
                veTextureIn.width = textureIn.width
                veTextureIn.height = textureIn.height
                veTextureIn.target = textureIn.target
                val veTextureOut = VideoEffect.VE_Texture()

                veTextureOut.textureID = textureOut.textureId
                veTextureOut.format = textureOut.format
                veTextureOut.width = textureOut.width
                veTextureOut.height = textureOut.height
                veTextureOut.target = textureOut.target

                VideoEffectJni.rendFrame(veTextureIn, veTextureOut)
            }

            override fun onInit() {
                VideoEffectJni.createContext()
            }

            override fun onRelease() {
            }

        })
        setContentView(mCameraView)
    }


}
